package com.star.carserviceapi.utils;

import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.star.carserviceapi.ClientApiConfig;
import com.star.carserviceapi.domain.request.base.BaseRequest;
import com.star.carserviceapi.domain.result.SendValidCodeResult;
import com.star.carserviceapi.domain.result.base.BaseResult;

public class HttpApiUtils {

	@SuppressWarnings("unchecked")
	public static <T extends BaseResult<?>> T invokeServer(BaseRequest request,
			T output) {

		HttpRequestUtils reqUtils = HttpRequestUtils.post(
				request.getActionUrl()).contentType("text/json;charset=utf-8");

		
		Log.i("http", request.getActionUrl());
		Log.i("http", request.getPostData().toString());
		
		int httpStatus = reqUtils.form(request.getPostData()).code();

		String response = reqUtils.body();

		Log.i("http", response);
		
		//String response = "{\"code\":200,\"data\":{\"mobile\":\"15395009950\"}}";

		// return new Gson().fromJson(response, new
		// TypeToken<SendValidCodeResult>() {
		// }.getType());
		return (T) new Gson().fromJson(response, output.getClass());

	}
}
